<?
function drizzo_get_the_link($id) {
	global $drizzo_links;
	
	$link_query = @mysql_query("SELECT * FROM `$drizzo_links` WHERE `ID`='$id' LIMIT 1");
	if(!mysql_errno()) {
		$link = drizzo_db_unwrap(@mysql_fetch_assoc($link_query));
		$link['added_raw'] = $link['added'];
		$date = drizzo_global_options('time_format');
		$link['added'] = date($date,$link['added']);
		return $link;
	} else {
		return false;
	}
} 

function drizzo_get_the_links($order = 'name') {
	global $drizzo_links;

	$links_query = @mysql_query("SELECT * FROM `$drizzo_links` ORDER BY `$order`");
	if(!mysql_errno()) {
		while($row = drizzo_db_unwrap(@mysql_fetch_assoc($links_query))) {
			$links[$i] = $row;
			$links[$i]['added_raw'] = $row['added'];
			$date = drizzo_global_options('time_format');
			$links[$i]['added'] = date($date,$row['added']);
			$i++;
		}
		return $links;
	} else {
		return false;
	}
} 

function drizzo_links_by_tag($tag, $limit = null, $order = 'name', $sort = 'ASC') {
	global $drizzo_links;
	
	if($limit) {
		$links_query = @mysql_query("SELECT * FROM `$drizzo_links` WHERE `tags` LIKE '%$tag%' ORDER BY `$order` $sort LIMIT $limit");
	} else {
		$links_query = @mysql_query("SELECT * FROM `$drizzo_links` WHERE `tags` LIKE '%$tag%' ORDER BY `$order` $sort");
	}
	if(!mysql_errno()) {
		while($row = drizzo_db_unwrap(@mysql_fetch_assoc($links_query))) {
			$links[$i] = $row;
			$links[$i]['added_raw'] = $row['added'];
			$date = drizzo_global_options('time_format');
			$links[$i]['added'] = date($date,$row['added']);
			$i++;
		}
		return $links;
	} else {
		return false;
	}
} 

function drizzo_links_by_user($user, $limit = null, $order = 'name', $sort = 'ASC') {
	global $drizzo_links;
	
	if($limit) {
		$links_query = @mysql_query("SELECT * FROM `$drizzo_links` WHERE `owner`='$user' ORDER BY `$order` $sort LIMIT $limit");
	} else {
		$links_query = @mysql_query("SELECT * FROM `$drizzo_links` WHERE `owner`='$user' ORDER BY `$order` $sort");
	}
	if(!mysql_errno()) {
		while($row = drizzo_db_unwrap(@mysql_fetch_assoc($links_query))) {
			$links[$i] = $row;
			$links[$i]['added_raw'] = $row['added'];
			$date = drizzo_global_options('time_format');
			$links[$i]['added'] = date($date,$row['added']);
			$i++;
		}
		return $links;
	} else {
		return false;
	}
} 

function drizzo_links_list($limit = null, $order = "ID", $list_type = "ul", $list_item_before = "<li>", $list_item_after = "</li>") {
	global $drizzo_links;
	
	if($limit) {
		$links_query = @mysql_query("SELECT * FROM `$drizzo_links` ORDER BY `$order` LIMIT $limit");
	} else {
		$links_query = @mysql_query("SELECT * FROM `$drizzo_links` ORDER BY `$order`");
	}
	if(!mysql_errno()) {
		$links = "<$list_type id=\"links_list\">\n";
		while($row = drizzo_db_unwrap(@mysql_fetch_assoc($links_query))) {
			if($limit == null) {
				$links .= "$list_item_before<a href=\"$row[url]\" title=\"$row[name]\">$row[name]</a>$list_item_after";
			} elseif($count < $limit) {
				$links .= "$list_item_before<a href=\"$row[url]\" title=\"$row[name]\">$row[name]</a>$list_item_after";
			}
			$count++;
		}
		$links .= "</$list_type>\n";
		return $links;
	} else {
		return false;
	}
} 

function drizzo_links_list_by_tag($tag, $limit = null, $order = 'ID', $list_type = "ul", $list_item_before = "<li>", $list_item_after = "</li>") {
	$links_query = drizzo_links_by_tag($tag, $limit, $order);
	if($links_query) {
		$links = "<$list_type id=\"links_list\">\n";
		while($row = drizzo_db_unwrap(@mysql_fetch_assoc($links_query))) {
			$links .= "$list_item_before<a href=\"$row[url]\" title=\"$row[name]\">$row[name]</a>$list_item_after";
		}
		$links .= "</$list_type>\n";
		return $links;
	} else {
		return false;
	}
} 

function drizzo_links_list_by_user($user, $limit = null, $order = 'ID', $list_type = "ul", $list_item_before = "<li>", $list_item_after = "</li>") {
	$links_query = drizzo_links_by_user($user, $limit, $order);
	if($links_query) {
		$links = "<$list_type id=\"links_list\">\n";
		while($row = drizzo_db_unwrap(@mysql_fetch_assoc($links_query))) {
			$links .= "$list_item_before<a href=\"$row[url]\" title=\"$row[name]\">$row[name]</a>$list_item_after";
		}
		$links .= "</$list_type>\n";
		return $links;
	} else {
		return false;
	}
} 

function drizzo_links_table($owner = null) {
	if($owner == null && !isset($_GET['user'])) {
		$links = drizzo_get_the_links();
	} elseif($owner == null) {
		$links = drizzo_links_by_user('0'); 
	} else {
		$links = drizzo_links_by_user($owner);
	}
	
	if($links) {
		$links_table = "<table>\n";
		$links_table .= "\t<tr id=\"first\">\n";
		$links_table .= "\t\t<th class=\"narrow center\">ID</th><th>Name</th><th>URL</th><th>Tags</th>";
		if($owner == null) {
			$links_table .= "<th>Owner</th>\n";
		}
		if(drizzo_user_is_allowed('links-publish')) {
			$links_table .= "<th colspan=\"2\" class=\"center\">Action</th>";
		}
		$links_table .= "\n\t</tr>\n";
		foreach($links as $link) {
			if($row_count%2) {
				$links_table .= "\t<tr>\n";
			} else {
				$links_table .= "\t<tr class=\"alt\">\n";
			}
			$links_table .= "\t\t<td class=\"center\">$link[ID]</td><td>$link[name]</td>";
			if(drizzo_validate_url($link['url'])) {
				$links_table .= "<td><a href=\"$link[url]\" title=\"$link[url]\">$link[url]</a></td>";
			} else {
				$links_table .= "<td class=\"alert\"><strong>$link[url] appears to be invalid</strong></td>";
			}
			$links_table .= "<td>" . drizzo_links_tags($link['tags']) . "</td>";
			if($owner == null && $link['owner'] == 0) {
				$links_table .= "<td>Site-wide</td>";
			} elseif($owner == null && $link['owner'] > 0) {
				$links_table .= "<td><a href=\"links.php?user=$link[owner]\" title=\"View Links by This User\">" . drizzo_user_info($link['owner'], 'nice_name') . "</a></td>";
			}
			if(drizzo_user_is_allowed('links-publish')) {
				$links_table .= "<td class=\"center\"><a href=\"links.php?edit=$link[ID]\" title=\"Edit this Link\"><img src=\"/drizzo/images/edit.png\" alt=\"Edit\" /></a></td><td class=\"center\"><a href=\"links.php?delete=$link[ID]\" title=\"Delete this Link\"><img src=\"/drizzo/images/delete.png\" alt=\"Delete\" /></a></td>";
			}
			$links_table .= "\n\t</tr>\n";
			$row_count++;
		}
		$links_table .= "</table>";
		return $links_table;
	} else {
		return false;
	}
} 

 function drizzo_links_tags($tags, $links = "true") {
	global $the_post;
	
	if($tags) {
		$tags = explode(", ", $tags);
		$tags_lower = array_map('strtolower', $tags);
		array_multisort($tags_lower, SORT_ASC, SORT_STRING, $tags);
		$tag_count = 1;
		$tags_string = null;
		foreach($tags as $tag) {
			$tag_nice = str_replace("-", " ", $tag);
			if($links == "true") {
				if($tag_count < count($tags)) {
					$tags_string .= "<a href=\"/tags/" . $tag . "\">" . $tag_nice . "</a>, ";
				} else {
					$tags_string .= "<a href=\"/tags/" . $tag . "\">" . $tag_nice . "</a>";
				}
			} else {
				if($tag_count < count($tags)) {
					$tags_string .= $tag_nice . ", ";
				} else {
					$tags_string .= $tag_nice;
				}
			}
			$tag_count++;
		}
		return $tags_string;
	} else {
		return false;
	}
}

function drizzo_links_users_dropdown() {
	global $drizzo_links, $user;
	
	$users_query = @mysql_query("SELECT `owner`, COUNT(*) AS `links_count` FROM `$drizzo_links` GROUP BY `owner`");
	if(!mysql_errno()) {
		while($row = @mysql_fetch_assoc($users_query)) {
			if($row['owner'] == 0 && $user <= 0 && isset($_GET['user'])) {
				$dropdown .= "\t<option value=\"links.php?user=$row[owner]\" selected>Site-wide ($row[links_count])</option>\n";
			} elseif($row['owner'] == 0 && ($user > 0 || !isset($_GET['user']))) {
				$dropdown .= "\t<option value=\"links.php?user=$row[owner]\">Site-wide ($row[links_count])</option>\n";
			} elseif($user == $row['owner']) {
				$dropdown .= "\t<option value=\"links.php?user=$row[owner]\" selected>" . drizzo_user_info($row['owner'], 'nice_name') . " ($row[links_count])</option>\n";
			} else {
				$dropdown .= "\t<option value=\"links.php?user=$row[owner]\">" . drizzo_user_info($row['owner'], 'nice_name') . " ($row[links_count])</option>\n";
			}
			$count = $count + $row['links_count'];
		}
		if(!$_GET['user']) {
			$dropdown = "\t<option value=\"links.php\" selected>All Links ($count)</option>\n" . $dropdown;
		} else {
			$dropdown = "\t<option value=\"links.php\">All Links ($count)</option>\n" . $dropdown;
		}
		$dropdown = "<select name=\"links_users_menu\" onchange=\"location = this.options[this.selectedIndex].value;\" style=\"width: 10em;\">\n" . $dropdown;
		$dropdown .= "</select>\n";
		return $dropdown;
	} else {
		return false;
	}
}

function drizzo_link_save() {
	global $drizzo_links;
	if($_POST) { // If a link has been submitted
		$_POST = drizzo_db_wrap($_POST);
		extract($_POST);
		$tags = drizzo_parse_the_tags($tags);
		if($submit == 'Save Edit') { // Update a link
			$links_query = @mysql_query("UPDATE `$drizzo_links` SET `name`='$name',`url`='$url',`tags`='$tags',`owner`='$owner',`added`='$added' WHERE `ID`='$id'");
			if(!mysql_errno()) {
				$response = "<p class=\"save\">" . drizzo_db_unwrap($name) . " has been saved.</p>";
			} else {
				$response = "<p class=\"alert\">" . drizzo_db_unwrap($name) . " could not be saved.<br>" . mysql_error() . "</p>";
			}
		} else { // Add a link
			$links_query = @mysql_query("INSERT INTO `$drizzo_links` SET `name`='$name',`url`='$url',`tags`='$tags',`owner`='$owner',`added`='$added'");
			if(!mysql_errno()) {
				$response = "<p class=\"save\">" . drizzo_db_unwrap($name) . " has been added.</p>";
			} else {
				$response = "<p class=\"alert\">" . drizzo_db_unwrap($name) . " could not be added.<br>" . mysql_error() . "</p>";
			}
		}
		return $response;
	} else { // If no information has been passed
		return false;
	}
}
?>
